﻿using System;
using System.Diagnostics;
using System.Windows;
using Hardcodet.Wpf.TaskbarNotification;
using Microsoft.Practices.Unity;
using StrategicDataSystems.Cloud.Monitoring.Configuration;
using StrategicDataSystems.Cloud.Monitoring.HostedServices;
using StrategicDataSystems.Cloud.Monitoring.UI.Services;
using StrategicDataSystems.Cloud.Monitoring.UI.ViewModels;

namespace StrategicDataSystems.Cloud.Monitoring.UI
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

        private TaskbarIcon _sysTrayIcon;

        private void App_Startup(object sender, StartupEventArgs ea)
        {
            this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_GlobalErrorHandler);

            _sysTrayIcon = (TaskbarIcon)FindResource("SysTrayIcon");
            IUnityContainer container = configureContainer();
            _sysTrayIcon.DataContext = container.Resolve<SysTrayViewModel>();
        }

        private void App_Exit(object sender, ExitEventArgs e)
        {
            _sysTrayIcon.Visibility = Visibility.Collapsed;
            _sysTrayIcon.Dispose();
        }

        void App_GlobalErrorHandler(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            string errorMessage = string.Format("An exception has occured: '{0}'.  Full Information: {1}", e.Exception.Message, e.Exception.ToString());
            Trace.TraceError(errorMessage);
            e.Handled = true;
        }

        private IUnityContainer configureContainer()
        {

            IHostedServiceManagement hostedServiceGateway = new HostedServiceManagementGateway();
            Settings settings = new Settings();
            ITimerService refreshTimerService = new RefreshTimerService();
            INotificationService notificationService = new SysTrayNotificationService((TaskbarIcon)FindResource("SysTrayIcon"));
            INetPresenceService netPresenceService = new InternetPresenceService();

            IUnityContainer container = new UnityContainer();

            container.RegisterInstance<IHostedServiceManagement>(hostedServiceGateway)
                    .RegisterInstance<Settings>(settings)
                    .RegisterInstance<INotificationService>(notificationService)
                    .RegisterInstance<ITimerService>(refreshTimerService)
                    .RegisterInstance<INetPresenceService>(netPresenceService)
                    .RegisterType<SysTrayViewModel>(
                        new InjectionConstructor(
                            container.Resolve<IHostedServiceManagement>(), 
                            container.Resolve<Settings>(),
                            container.Resolve<INotificationService>(),
                            container.Resolve<ITimerService>(),
                            container.Resolve<INetPresenceService>()));


            return container;

        }

    }
}
